package org.deng.learn.concurrent.basic;



import java.lang.reflect.Field;

/**
 * @author 邓力宾
 * @date 2020/5/16 下午2:56
 * @desc
 */
public class UnsafeDemo {

    /*private volatile int data;
    private volatile  Object o;
    private static final   long oOffset;
    private static final sun.misc.Unsafe UNSAFE;
    static{
        try {
            Field theUnsafeField = Unsafe.class.getDeclaredField("theUnsafe"); //反射拿到该字段
            theUnsafeField.setAccessible(true);
            UNSAFE = (Unsafe) theUnsafeField.get(null); // 静态字段
            oOffset = UNSAFE.objectFieldOffset // 拿到字段的偏移量
                    (UnsafeDemo.class.getDeclaredField("o"));
        } catch (Exception e) {
            throw new Error(e);
        }
    }
    public static void main(String[] args) {
        UnsafeDemo unsafeDemo = new UnsafeDemo();
        Object o = new Object();
        UNSAFE.compareAndSwapObject(unsafeDemo, oOffset, null, o); // 根据字段偏移量设置字段的值
        System.out.println(o);
        System.out.println(unsafeDemo.o);
    }*/


}
